package com.iflytek.voicedemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.iflytek.sunflower.FlowerCollector;
import com.iflytek.utils.VoiceRecognitionManager;
import com.iflytek.view.VoiceRecognitionView;

public class OSPTestActivity extends Activity implements OnClickListener, VoiceRecognitionManager.SpeechRecognitionCallBack {

    private static final String TAG = OSPTestActivity.class.getSimpleName();
    private Toast mToast;
    private VoiceRecognitionView voiceview;
    private Button button;
    private VoiceRecognitionManager manager;


    @SuppressLint("ShowToast")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_xunfei_voicetest);
        requestPermissions();
        mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        //view
        voiceview = (VoiceRecognitionView) findViewById(R.id.voiceview);
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
        voiceview.setSpeechRecognitionCallBack(this);

        //manager(可以直接使用 manager 不使用view)
        manager = new VoiceRecognitionManager(this);
        manager.setVoiceRecognitionCallBack(this);

    }

    @Override
    public void onClick(View view) {
        manager.startRecognition();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.release();
        voiceview.release();
    }

    private void showTip(final String str) {
        mToast.setText(str);
        mToast.show();
    }

    @Override
    protected void onResume() {
        // 开放统计 移动数据统计分析
        FlowerCollector.onResume(OSPTestActivity.this);
        FlowerCollector.onPageStart(TAG);
        super.onResume();
    }

    @Override
    protected void onPause() {
        // 开放统计 移动数据统计分析
        FlowerCollector.onPageEnd(TAG);
        FlowerCollector.onPause(OSPTestActivity.this);
        super.onPause();
    }

    private void requestPermissions() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int permission = ActivityCompat.checkSelfPermission(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]
                            {Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.LOCATION_HARDWARE, Manifest.permission.READ_PHONE_STATE,
                                    Manifest.permission.WRITE_SETTINGS, Manifest.permission.READ_EXTERNAL_STORAGE,
                                    Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_CONTACTS}, 0x0010);
                }

                if (permission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION}, 0x0010);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    public void onStartOfSpeech() {

    }

    @Override
    public void onEndOfSpeech() {

    }

    @Override
    public void onSuccess(String text,boolean isLast) {
        Log.d(TAG, "onSuccess: "+Thread.currentThread().getName().toString());
        showTip(text);
        //manager.release();
    }

    @Override
    public void onFail(String failMsg) {
        showTip(failMsg);
        //manager.release();
    }
}
